package tech.yingchuan.blogs.controller.after;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.yingchuan.blogs.common.ResponseResult;
import tech.yingchuan.blogs.common.TableDataGridResult;
import tech.yingchuan.blogs.model.Acl;
import tech.yingchuan.blogs.param.AclParam;
import tech.yingchuan.blogs.service.AclService;

import java.util.Arrays;
import java.util.List;

/**
 * 资源（菜单）后台管理页面的控制层
 * @author dingguangyi
 * @version 1.0
 * @date 2018年6月23日
 */
@RequestMapping("/after/acl/")
@Controller
public class AclController {

    @Autowired
    private AclService aclService;
    /**
     * 跳转权限页面
     * @return
     */
    @RequestMapping("acl.html")
    public String aclPage(){
        return "after/sys/acl/acl";
    }

    /**
     * 跳转权限添加页面
     * @return
     */
    @RequestMapping("aclAdd.html")
    public String aclAddPage(){
        return "after/sys/acl/aclAdd";
    }

    /**
     * 跳转权限添加页面
     * @return
     */
    @RequestMapping("aclEdit.html")
    public String aclEditPage(String aclId,Model model){
        model.addAttribute("aclId",aclId);
        return "after/sys/acl/aclAdd";
    }

    @RequestMapping("getIdAcl.json")
    @ResponseBody
    public ResponseResult getIdAcl(String id){
        Acl acl=aclService.getAcl(id);
        return ResponseResult.success(acl);
    }

    /**
     * 获取权限列表
     * @param aclParam
     * @return
     */
    @RequestMapping(value = "/listAcl.json", method =
            RequestMethod.GET)
    @ResponseBody
    public TableDataGridResult<Acl> listAcl(AclParam aclParam){
         List<Acl> acls = aclService.listAcl(aclParam);
        return TableDataGridResult.TableDataGridResult(acls);
    }

    /**
     *
     * @return
     */
    @RequestMapping(value = "/aclParentTree.json")
    @ResponseBody
    public ResponseResult<List<Acl>> listAclParentTree(){
        return ResponseResult.success(aclService.listAclParentTree(),"数据查询成功");
    }

    @RequestMapping(value = "/saveAcl.json",method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult saveAcl(AclParam aclParam){
        aclService.saveAcl(aclParam);
        return ResponseResult.successMsg("数据保存成功！");
    }

    @RequestMapping(value = "/updateAcl.json",method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult updateAcl(AclParam aclParam){
        aclService.updateAcl(aclParam);
        return ResponseResult.successMsg("数据更新成功！");
    }

    @RequestMapping(value = "/deleteAcl.json",method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult deleteAcl(@RequestParam("ids") String ids){
        List<String> asList=Arrays.asList(ids.split(","));
        aclService.removeBatchIdAcl(asList);
        return ResponseResult.successMsg("数据删除成功!");
    }
}
